#!/usr/bin/env python
#-*- coding:utf8 -*-
import os,web
from config.routes import *
web.config.debug = True
web.config.session_parameters['cookie_name']        = '_fox'
web.config.session_parameters['cookie_domain']      = None
web.config.session_parameters['timeout']            = 86400
web.config.session_parameters['ignore_expiry']      = True
web.config.session_parameters['ignore_change_ip']   = True
web.config.session_parameters['secret_key']         = '1234567890asdfghjkl;qwertyuiopzx'
web.config.session_parameters['expired_message']    = '_sess'
root = os.path.realpath(os.path.dirname(__file__))
spath= web.session.DiskStore('sessions')
app  = web.application(urls, globals())
sess = web.session.Session(app, spath, initializer = {'_login':False})
#程序入口
class Import:
    def __init__(self):
        pass
    def _request(self,args,_type):
        module,action,argv = 'statics','index',False
        if len(args) > 0:
            module = args[0]
        if len(args) > 1:
            action  = args[1]
        if len(args) > 2:
            argv = args[2:]
        try:
            _module = __import__('app.controllers.'+module+'_controller', {}, {}, [module])
        except:
            return 'can not found module %s' % module
        try:
            _object = getattr(_module, module.capitalize()+'Controller')(module)
        except Exception ,e:
            return e.message
        if _type == 'get':
            if hasattr(_object,action):
                try:
                    if argv is False:
                        return getattr(_object, action)()
                    return getattr(_object, action)(argv)
                except Exception ,e:
                    return e.message
            else:
                return 'can not found %s/%s' % (module,action)
        else:
            if hasattr(_object,'_'+action):
                try:
                    if argv is False:
                        return getattr(_object, '_'+action)()
                    return getattr(_object, '_'+action)(argv)
                except Exception ,e:
                    return e.message
            else:
                return 'can not found %s/%s' % (module,action)
    def GET(self,*args):
        return self._request(args,'get')
    def POST(self, *args):
        return self._request(args,'post')
